<?php

class Home extends HomeController {

    public function POST_index($login, $pwd) {

        $this->Authenticate($login, $pwd);

        if (isset($this->user)) {
            $this->view('home/menu');
        } else {
            $this->error = new Exception(Strings::MESSAGE_0020);
            $this->view('home/index');
        }

        // purge le cache
        delete_files();
    }

    private function Authenticate($login, $pwd) {

        global $REQUEST_001;

        $data = db_request_select($REQUEST_001, array(':login' => $login, ':pwd' => $pwd));
        $datarow = $data[0];

        if ($datarow === FALSE) {
            return;
        }

        $this->user = $this->model('User');
        $this->user->load($datarow['id_user']/* , $datarow['id_compte_client'] */);

        $_SESSION['user'] = $this->user->get_id_user();
    }

    /* ================================================================================================
      fonction               :   logout
      description            :   déconnecte le user en cours de connexion et doit
      être en charge des opérations finales, tel que pour l'instant:
      -   mettre la variable de session à vide et la détruire
      ================================================================================================ */

    public function GET_logout() {

        // purge le cache
        delete_files('png', '5');
        delete_files('csv', '5');

        // From the session_destroy() page in the PHP manual:
        // http://php.net/session_destroy
        // Initialize the session.
        // If you are using session_name("something"), don't forget it now!
        if (!isset($_SESSION)) {
            session_start();
        }
        // Unset all of the session variables.
        $_SESSION = array();

        // If it's desired to kill the session, also delete the session cookie.
        // Note: This will destroy the session, and not just the session data!
        if (ini_get("session.use_cookies")) {
            $params = session_get_cookie_params();
            setcookie(session_name(), '', time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"]
            );
        }

        // Finally, destroy the session.
        session_destroy();

        $this->view('home/index');
    }

}
